package com.asa.utils;

/**
 * 一般性方法,类似于待定系数，函数拟合
 * 这种方式的拟合度是比较好的
 * @author Administrator
 * 
 */
public class Norm {
	/**
	 * Fit a function of the form y = a ln x + b cos x + cex to the following
	 * table values: x 0.24 0.65 0.95 1.24 1.73 2.01 2.23 2.52 2.77 2.99 y 0.23
	 * −0.26 −1.10 −0.45 0.27 0.10 −0.29 0.24 0.56 1.00
	 * 最终结果
	 * y = −1.04103 ln x − 1.26132 cos x + 0.03073ex
	 * @param args
	 */
	public static double[] nihe(double[] x ,double[] y) {

		double[][] a = new double[3][3];
		double[] b = new double[3];
//		double[] x = { 0.24, 0.65, 0.95, 1.24, 1.73, 2.01, 2.23, 2.52, 2.77,
//				2.99 };
//		double[] y = { 0.23, -0.26, -1.10, -0.45, 0.27, 0.10, -0.29, 0.24,
//				0.56, 1.00 };
		
		a[0][0] = oneone(x);
		a[0][1] = onetwo(x);
		a[0][2] = onethree(x);
		a[1][0] = twoone(x);
		a[1][1] = twotwo(x);
		a[1][2] = twothree(x);
		a[2][0] = threeone(x);
		a[2][1] = threetwo(x);
		a[2][2] = threethree(x);
		
		
		b[0] = one(x, y);
		b[1] = two(x, y);
		b[2] = three(x, y);
		
		
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a.length; j++) {
				System.out.print(a[i][j]+"  ");
			}
			//System.out.println();
		}
		//System.out.println("=================");
		for (int i = 0; i < b.length; i++) {
			System.out.print(b[i]+"  ");
		}
		//System.out.println("解这个方程组再回调将参数回填就可得函数拟合");
		
		double[] duoyuanyici = MathUtils.duoyuanyici(a, b);
		
		return duoyuanyici;
	}

	public static double oneone(double[] x) {
		
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+Math.log(x[i])*Math.log(x[i]);
		}
		
		
		return all;
	}

	public static double onetwo(double[] x) {
		
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+Math.log(x[i])*Math.cos(x[i]);
		}
		
		return all;
	}

	public static double onethree(double[] x) {
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+Math.log(x[i])*Math.exp(x[i]);
		}
		
		return all;
	}

	
	public static double twoone(double[] x) {

		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+Math.log(x[i])*Math.cos(x[i]);
		}
		
		return all;
	}

	public static double twotwo(double[] x) {
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+Math.cos(x[i])*Math.cos(x[i]);
		}
		
		return all;
	}
	public static double twothree(double[] x) {
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+Math.cos(x[i])*Math.exp(x[i]);
		}
		
		return all;
	}


	public static double threeone(double[] x) {
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+Math.log(x[i])*Math.exp(x[i]);
		}
		
		return all;
	}

	public static double threetwo(double[] x) {
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+Math.cos(x[i])*Math.exp(x[i]);
		}
		
		return all;
	}

	public static double threethree(double[] x) {
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+Math.exp(x[i])*Math.exp(x[i]);
		}
		
		return all;
	}
	
	public static double one(double[] x,double[] y) {
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+y[i]*Math.log(x[i]);
		}
		
		return all;
	}
	public static double two(double[] x,double[] y) {
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+y[i]*Math.cos(x[i]);
		}
		
		return all;
	}
	public static double three(double[] x,double[] y) {
		double all = 0;
		for (int i = 0; i < x.length; i++) {
			all = all+y[i]*Math.exp(x[i]);
		}
		
		return all;
	}
	
}
